﻿@{
    ViewData["Title"] = "EventEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
    <body>
        <form class="layui-form" action="" style="align-content:center" onsubmit="return false">
            <div class="layui-form-item">
                <label class="layui-form-label">父节点类型</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="TypeInfo" name="TypeInfo" lay-filter="aihao">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型编码</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="TypeId" name="TypeId" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型名称</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="TypeName" name="TypeName" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="EventDesc" name="EventDesc" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-inline layui-input-wrap">
                    <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                        <i class="layui-icon layui-icon-upload"></i> 单图片上传
                    </button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="Image" name="Image" style="width: 100%; height: 92px;">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">事件级别</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="EventLevel" name="EventLevel" lay-filter="aihao">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">处置时限</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="EventTimeLimit" name="EventTimeLimit" lay-filter="aihao">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-btn-container" style="text-align:center">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">保存</button>
            </div>
        </form>
    @* <script src="~/js/event/EventUpd.js?n=" +new Date()"></script> *@
    
    </body>
    <script>

    layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table', 'upload', 'dropdown'], function () {
        var form = layui.form;
        var upload = layui.upload;
        var table = layui.table;
        var dropdown = layui.dropdown;
        var laypage = layui.laypage;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var layer = layui.layer; // 添加layui弹出层模块
        var Id = location.search.substring(2).split("=")[1];

        $(function () {
            GetInfo();
        })
        function GetInfo() {
            $.ajax({
                url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventGet?Id=" + Id,
                type: "get",
                headers: {
                    'Authorization': "Bearer " + localStorage["token"] + ""
                },
                success: function (res) {
                    clickTree();
                    $("#TypeInfo").val(res.PId);
                    $("#TypeId").val(res.TypeId);
                    $("#TypeName").val(res.TypeName);
                    $("#EventDesc").val(res.EventDesc);
                    $("#Image").val(res.EventImage);
                    SelLevel();
                    $("#EventLevel").val(res.EventLevel);
                    SelTime();
                    $("#EventTimeLimit").val(res.EventTimeLimit);
                    form.render('select');
                }
            })
        }
        function SelLevel() {
            $.ajax({
                url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleLevel",
                async: false,
                type: "get",
                headers: {
                    'Authorization': "Bearer " + localStorage["token"] + ""
                },
                success: function (res) {
                    var select = $("#EventLevel");
                    $.each(res, function (index, item) {
                        select.append($('<option>').val(item.LevelName).text(item.LevelName))
                    })
                    layui.form.render('select');
                }
            })
        }
        function SelTime() {
            $.ajax({
                url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleTimeLimit",
                async: false,
                type: "get",
                headers: {
                    'Authorization': "Bearer " + localStorage["token"] + ""
                },
                success: function (res) {
                    var select = $("#EventTimeLimit");
                    $.each(res, function (index, item) {
                        select.append($('<option>').val(item.LimitName).text(item.LimitName))
                    })
                    layui.form.render('select');
                }
            })
        }
        function clickTree() {
            $.ajax({
                url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventSelect",
                async: false,
                type: "get",
                headers: {
                    'Authorization': "Bearer " + localStorage["token"] + ""
                },
                success: function (res) {
                    var select = $("#TypeInfo");
                    $.each(res, function (index, item) {
                        select.append($('<option>').val(item.EventTypeName).text(item.EventTypeName));
                    })
                    layui.form.render('select');
                    //设置对应 id 的节点选中
                }
            })
        }
        upload.render({
            elem: '#ID-upload-demo-btn',
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            url: 'http://8.140.245.229:8000/webroute/api/Event/UpFile', // 实际使用时改成您自己的上传接口即可。
            done: function (res) {

                $('#Image').attr('src', 'http://8.140.245.229:8000/webroute/File/' + res);
                console.log($('#Image'));
            },
        })

        form.on('submit(demo1)', function (data) {
            var obj = {};
            var field = data.field;
            obj.EventId = Id;
            obj.PId = field.TypeInfo;
            obj.TypeId = field.TypeId;
            obj.TypeName = field.TypeName;
            obj.EventDesc = field.EventDesc;
            obj.EventImage = field.Image;
            obj.EventLevel = field.EventLevel;
            obj.EventTimeLimit = field.EventTimeLimit;
            $.ajax({
                url: "http://8.140.245.229:8000/webroute/api/Event/HandleUpdEvent",
                type: "put",
                headers: {
                    'Authorization': "Bearer " + localStorage["token"] + ""
                },
                data: JSON.stringify(obj),
                contentType: "application/json",
                success: function (res) {
                    if (res > 0) {
                        layer.confirm('修改成功', {
                            btn: ['确定'] //按钮
                        }, function () {
                            location.href = "/Event/EventManage";
                        });

                    } else {
                        layer.alert("修改失败");
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        })
    })
    function RestEmployee() {
        $(".layui-form").reset();
        layui.form.render();
    }
    </script>
</html>
